<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="单号" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="主题" prop="prTitle">
        <el-input
          v-model="queryParams.prTitle"
          placeholder="请输入主题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="制单人ID" prop="operator">
        <el-input
          v-model="queryParams.operator"
          placeholder="请输入制单人ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="请购日期" prop="prDate">
        <el-date-picker clearable
          v-model="queryParams.prDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择请购日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="请购人ID" prop="prMan">
        <el-input
          v-model="queryParams.prMan"
          placeholder="请输入请购人ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目" prop="projectId">
        <el-select v-model="queryParams.projectId" filterable clearable placeholder="请选择项目">
          <el-option
            v-for="item in projectsList"
            :key="item.id"
            :label="item.projectName"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['purmgt:pr:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['purmgt:pr:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['purmgt:pr:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['purmgt:pr:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="prList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"  fixed="left"/>
      <el-table-column label="单号" align="center" prop="code" />
      <el-table-column label="主题" align="center" prop="prTitle" />
      <el-table-column label="制单人ID" align="center" prop="operator" />
      <el-table-column label="请购日期" align="center" prop="prDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.prDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="请购人ID" align="center" prop="prMan" />
      <el-table-column label="项目ID" align="center" prop="projectId" >
        <div slot-scope="scope">
          {{
            projectsList.find(item => {
              if (item.id == scope.row.projectId)
                return item
            }) && projectsList.find(item => {
              if (item.id == scope.row.projectId)
                return item
            }).projectName || ''
          }}
        </div>
      </el-table-column>
      <el-table-column label="采购单状态" align="center" prop="purStatus" />
      <el-table-column label="单据状态" align="center" prop="docStatus" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="150px">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['purmgt:pr:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['purmgt:pr:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改请购单 对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="90%" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="110px" :inline="true">
        <el-form-item label="单号" prop="code">
          <el-input v-model="form.code" placeholder="请输入单号" />
        </el-form-item>
        <el-form-item label="主题" prop="prTitle">
          <el-input v-model="form.prTitle" placeholder="请输入主题" />
        </el-form-item>
        <el-form-item label="制单人ID" prop="operator">
          <el-input v-model="form.operator" placeholder="请输入制单人ID" />
        </el-form-item>
        <el-form-item label="请购日期" prop="prDate">
          <el-date-picker clearable
            v-model="form.prDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择请购日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="请购人ID" prop="prMan">
          <el-input v-model="form.prMan" placeholder="请输入请购人ID" />
        </el-form-item>
        <el-form-item label="项目" prop="projectId">
          <el-select v-model="form.projectId" filterable placeholder="请选择项目">
            <el-option
              v-for="item in projectsList"
              :key="item.id"
              :label="item.projectName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="附件">
          <file-upload v-model="form.attachmentFile"/>
        </el-form-item>
        <el-divider content-position="center">请购单明细 信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddPsiPurchaseReqDetail">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeletePsiPurchaseReqDetail">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="psiPurchaseReqDetailList" :row-class-name="rowPsiPurchaseReqDetailIndex" @selection-change="handlePsiPurchaseReqDetailSelectionChange" ref="psiPurchaseReqDetail">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="物料" prop="goodsId" >
            <template slot-scope="scope">
              <el-select v-model="scope.row.goodsId" filterable placeholder="请输入产品名称" style="width:100%"
                         @change="event=>{selectChange(scope.row,event)}">
                <template v-for="item in goodsList">
                  <el-option :label="item.name" :value="item.id">
                  </el-option>
                </template>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="批次" prop="goodsBatch" >
            <template slot-scope="scope">
              <el-input v-model="scope.row.goodsBatch" placeholder="请输入批次" disabled/>
            </template>
          </el-table-column>
          <el-table-column label="规格" prop="spec" >
            <template slot-scope="scope">
              <el-input v-model="scope.row.spec" placeholder="请输入规格" disabled/>
            </template>
          </el-table-column>
          <el-table-column label="请购数量" prop="prNum" >
            <template slot-scope="scope">
              <el-input v-model="scope.row.prNum" placeholder="请输入请购数量" />
            </template>
          </el-table-column>
          <el-table-column label="已采购数量" prop="qpNum" >
            <template slot-scope="scope">
              <el-input v-model="scope.row.qpNum" placeholder="请输入已采购数量" />
            </template>
          </el-table-column>
          <el-table-column label="需要日期" prop="requestDate" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.requestDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择需要日期" />
            </template>
          </el-table-column>
          <el-table-column label="备注" prop="remark" >
            <template slot-scope="scope">
              <el-input v-model="scope.row.remark" placeholder="请输入备注" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPr, getPr, delPr, addPr, updatePr } from "@/api/purmgt/pr";
import {goodsListAll} from "../../../api/basic/goods";
import {goodsCtgListAll} from "../../../api/basic/goodsctg";
import {projectsDropdownList} from "../../../api/prdmgt/project";

export default {
  name: "Pr",
  data() {
    return {
      projectsList: [],//项目下拉
      goodsList: [],//物料下拉
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedPsiPurchaseReqDetail: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 请购单 表格数据
      prList: [],
      // 请购单明细 表格数据
      psiPurchaseReqDetailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,        prTitle: null,        operator: null,        prDate: null,        prMan: null,        projectId: null,        purStatus: null,        docStatus: null,        attachmentFile: null,        createId: null,        updateId: null,      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.goodsListAllFn();
    this.getProjectsList();
  },
  methods: {
    /** 获取项目下拉列表 **/
    async getProjectsList() {
      await projectsDropdownList().then(res => {
        this.projectsList = res.data
      })
    },
    /** 查询请购单 列表 */
    getList() {
      this.loading = true;
      listPr(this.queryParams).then(response => {
        this.prList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,        code: null,        prTitle: null,        operator: null,        prDate: null,        prMan: null,        projectId: null,        purStatus: "0",        docStatus: "0",        attachmentFile: null,        remark: null,        delFlag: null,        createId: null,        createBy: null,        createTime: null,        updateId: null,        updateBy: null,        updateTime: null      };
      this.psiPurchaseReqDetailList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加请购单 ";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPr(id).then(response => {
        this.form = response.data;
        this.psiPurchaseReqDetailList = response.data.psiPurchaseReqDetailList;
        this.open = true;
        this.title = "修改请购单 ";
      });
    },
    selectChange(row,value) {
      let item = this.goodsList.find(d => d.id == value)
      row.goodsBatch = item.goodsBatch&& item.goodsBatch || ''
      row.spec = item.spec&& item.spec || ''
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.psiPurchaseReqDetailList = this.psiPurchaseReqDetailList;
          if (this.form.id != null) {
            updatePr(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPr(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除请购单 编号为"' + ids + '"的数据项？').then(function() {
        return delPr(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 请购单明细 序号 */
    rowPsiPurchaseReqDetailIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 请购单明细 添加按钮操作 */
    handleAddPsiPurchaseReqDetail() {
      let obj = {};
      obj.goodsId = "";
      obj.goodsBatch = "";
      obj.spec = "";
      obj.prNum = "";
      obj.qpNum = "";
      obj.requestDate = "";
      obj.remark = "";
      obj.createId = "";
      obj.updateId = "";
      this.psiPurchaseReqDetailList.push(obj);
    },
    /** 请购单明细 删除按钮操作 */
    handleDeletePsiPurchaseReqDetail() {
      if (this.checkedPsiPurchaseReqDetail.length == 0) {
        this.$modal.msgError("请先选择要删除的请购单明细 数据");
      } else {
        const psiPurchaseReqDetailList = this.psiPurchaseReqDetailList;
        const checkedPsiPurchaseReqDetail = this.checkedPsiPurchaseReqDetail;
        this.psiPurchaseReqDetailList = psiPurchaseReqDetailList.filter(function(item) {
          return checkedPsiPurchaseReqDetail.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handlePsiPurchaseReqDetailSelectionChange(selection) {
      this.checkedPsiPurchaseReqDetail = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('purmgt/pr/export', {
        ...this.queryParams
      }, `pr_${new Date().getTime()}.xlsx`)
    },
    async goodsListAllFn() {
      await goodsListAll().then(response => {
        this.goodsList = response.data;

      })
    },
  }
};
</script>
